package main

import (
	"fmt"
	"sync"
)

var mutex sync.Mutex
var count = 0
var wg sync.WaitGroup
var RWmutex sync.RWMutex

// 互斥锁
func fn1() {
	mutex.Lock()
	defer mutex.Unlock()
	count++
	fmt.Println("count: ", count)
	wg.Done()
}

// 读写互斥锁
func write() {
	fmt.Println("write")
	wg.Done()
}

func read() {
	RWmutex.RLock()
	defer RWmutex.RUnlock()
	count++
	fmt.Println("count: ", count)
	wg.Done()
}
func main() {

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go fn1()
	}
	wg.Wait()
	println(count)

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go write()
	}
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go read()
	}
	wg.Wait()
	println(count)
}
